//package com.example.demo02.service.Impl;
//
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.example.demo02.entity.Menu;
//import com.example.demo02.entity.Role;
//import com.example.demo02.entity.TUser;
//import com.example.demo02.mapper.TUserMapper;
//import com.example.demo02.service.TUserService;
//import jakarta.annotation.Resource;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.authority.SimpleGrantedAuthority;
//import org.springframework.security.core.userdetails.User;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.stereotype.Service;
//
//import java.util.ArrayList;
//import java.util.Collection;
//
////登录的业务逻辑
///*
//* User 是由 Spring Security 提供的类，用于登录认证，跟用户自定义的 TUser 区别开来
//* */
//@Service
//public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements TUserService {
//
//    @Resource
//    TUserMapper userMapper;
//
//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        TUser tuser=userMapper.findUserByName(username);//从数据库中查询用户
//        if(tuser==null){
//            throw new UsernameNotFoundException("帐户不存在");
//        }
//        tuser.setRoles(userMapper.findRolesByUserId(tuser.getUid()));//从数据库中查询到该用户的所有角色（含权限）
//        Collection<GrantedAuthority> authorities = new ArrayList<>();
//        for(Role role:tuser.getRoles()){//取出用户的角色，封装到authorities中
//            authorities.add(new SimpleGrantedAuthority(role.getName()));
//            for(Menu menu:role.getMenus()){ //如果权限精确到权限菜单级别，则要补充这个
//                authorities.add(new SimpleGrantedAuthority(menu.getMenuname()));
//            }
//        }
//        //下面的密码一般要加密
//        return new User(tuser.getUsername(),new BCryptPasswordEncoder().encode(tuser.getPassword()),authorities);
//
//    }
//}
